package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.dao.LinkDao;
import com.ljx.entity.Banner;
import com.ljx.entity.Link;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：lmy
 * @Date：2023/3/1 15:28
 */
@RestController
@RequestMapping("home/api/link")
public class ApiLinkController {

    @Autowired(required = false)
    private LinkDao linkDao;

    @PostMapping("getAllLink")
    public Result getAllLink(@RequestParam Integer currentPage, @RequestParam Integer pageSize,
                                 @RequestBody Link link){

        Page<Link> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        if (link.getLinkName() != null){
            wrapper.like("link_name",link.getLinkName());
        }
        if (link.getIsDisable() != null){
            wrapper.like("is_disable",link.getIsDisable());
        }
        Page<Link> linkPage = linkDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",linkPage);

    }

    @RequestMapping("changeStatus")
    public Result changeStatus(@RequestParam String linkId,@RequestParam Integer isDisable){
        Link link = new Link();
        link.setLinkId(linkId);
        link.setIsDisable(isDisable);
        int row = linkDao.updateById(link);
        if (row == 1){
            return new Result(2000,"修改状态成功");
        }
        return  new Result(5000,"修改状态失败");
    }

    @RequestMapping("deleteLink")
    public Result deleteLink(@RequestParam String linkId){
        int row = linkDao.deleteById(linkId);
        if (row == 1){
            return new Result(2000,"删除成功");
        }
        return  new Result(5000,"删除失败");
    }

    @PostMapping("addLink")
    public Result addLink(@RequestBody Link link){
        int row = linkDao.insert(link);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return  new Result(5000,"添加失败");
    }

    @PostMapping("editLink")
    public Result editLink(@RequestBody Link link){
        int row = linkDao.updateById(link);
        if (row == 1){
            return new Result(2000,"修改成功");
        }
        return  new Result(5000,"修改失败");
    }


}
